// 重量校验
import Base from './base'

class Weight {
  rule ({ data, number, isNull, max }) { // 重量校验
    const num = number !== undefined ? number : 3
    const maxLength = max !== undefined ? max : 9999.999

    if (!Base.checkNumber(Number(data))) {
      return { flag: false, data: '不是数字' }
    }

    if (!isNull) {
      if (Number(data) < 0) {
        return { flag: false, data: '数值必须大于0' }
      }
    } else {
      if (Number(data) < 0) {
        return { flag: false, data: '数值必须大于或等于0' }
      }
    }
    if (!isNull && Number(data) === 0) {
      return { flag: false, data: '数值必须大于0' }
    }
    if (!Base.checkDecimalsPlaces(Number(data), num)) {
      return { flag: false, data: `小数不能超过${num}位` }
    }
    if (maxLength) {
      if (Number(data) > maxLength) {
        return { flag: false, data: `数值不能大于${maxLength}` }
      }
    }

    return { flag: true, data: '通过' }
  }
}
export default new Weight()
